<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        // 创建对象方法
        // 1.字面量
        var obj = {
            name: 'mingda',
            sex: '男',
            age: 18,
            sayHi: function() {
                console.log('hi');
            }
        };
        // 使用对象
        console.log(obj.age);
        console.log(obj['sex']);
        obj.sayHi();
        
        // 2.利用new
        var obj1 = new Object();
        obj1.name = 'di';
        obj1.age = 19;
        obj1.sayHello = function() {
            console.log('hello');
        }
        // 使用对象
        alert(obj1.name);
        alert(obj1['sex']);
        obj1.sayHello();

        //3.利用构造函数
        function King(name, sex, age) {
            this.name = name;
            this.sex = sex;
            this.age = age;
            this.say = function() {
                console.log('hi');
            }
        }
        bob = new King('bob', 'man', 19);
        alert(bob.name);
        al = new King('alice', 'woman', 20);
        al.say();
        
        // 遍历对象
        var obj2 = {
            name: 'mingda',
            sex: 'man',
            age: 18,
            fun: function() {
                console.log('hi')
            }
                    
        };

        for(var k in obj2) {
            // 输出对象属性
            console.log(k);
            // 输出对象值
            console.log(obj2[k]);
        }


    </script>
</head>
<body>
    
</body>
</html>